//
//  MTProfileViewController.m
//  ShiShiBang
//
//  Created by 宋宝强 on 16/9/23.
//  Copyright © 2016年 motianjituan. All rights reserved.
//

#import "MTProfileViewController.h"
#import "XLWaveView.h"

@interface MTProfileViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

/** 数据数组 */
@property (nonatomic, strong) NSArray *dataList;

/** 记录清空缓存的index */
@property (nonatomic, strong) NSIndexPath *path;

/** 头View */
@property (nonatomic, weak) XLWaveView *waveView;

/** 是否正在播放动画 */
@property (nonatomic, assign, getter=isShowWave) BOOL showWave;


@end

@implementation MTProfileViewController

#pragma mark - lazy loading...

- (NSArray *)dataList{
    if (!_dataList) {
        NSMutableDictionary *photo = [NSMutableDictionary dictionary];
        photo[@"title"] = @"个人相册";
        
        
        //自己写要跳转到的控制器
//        miaoBi[@"controller"] = [XLOtherViewController class];
        
        NSMutableDictionary *renzheng = [NSMutableDictionary dictionary];
        renzheng[@"title"] = @"认证信息";
               //自己写要跳转到的控制器
//        zhiBoJian[@"controller"] = [XLOtherViewController class];
        
        NSMutableDictionary *like = [NSMutableDictionary dictionary];
        like[@"title"] = @"已获得攒";

        NSMutableDictionary *shouYi = [NSMutableDictionary dictionary];
        shouYi[@"title"] = @"发布的技能";
//        shouYi[@"icon"] = @"MoreMyBankCard";
//        shouYi[@"controller"] = [XLOtherViewController class];
        
        
        NSArray *section1 = @[photo, renzheng,like];
        NSArray *section2 = @[shouYi];
     
        
        _dataList = [NSArray arrayWithObjects:section1, section2, nil];
    }
    return _dataList;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStyleGrouped];
        _tableView.mt_y = -20;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

//刷新tableview
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.tableView reloadData];
    
    UINavigationBar *nav = self.navigationController.navigationBar;
    nav.hidden = YES;
    
}


#pragma mark - life cycle...

- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self setupHeaderView];
    
    
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)setupHeaderView
{
    
    XLWaveView *wave = [[XLWaveView alloc] initWithFrame:CGRectMake(0, -60, MTScreenW, 270) Image:@"Cyuri03" centerIcon:@"icon"];
    self.waveView = wave;
    [self scrollViewDidScroll:self.tableView];
    
    
    [self.tableView addSubview:wave];
    //    self.tableView.tableHeaderView = wave;
    
    // 与图像高度一样防止数据被遮挡
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MTScreenW , wave.mt_height)];
    
}



#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataList.count ;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataList[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *ID = @"mineCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    NSDictionary *dict = self.dataList[indexPath.section][indexPath.row];
    cell.textLabel.text = dict[@"title"];
    cell.imageView.image = [UIImage imageNamed:dict[@"icon"]];
    
    if (!indexPath.section && !indexPath.row) {
        cell.detailTextLabel.text = @"270枚";
//        cell.detailTextLabel.textColor = XLColor(0.935, 210, 0);
    }
    
    cell.selected = YES;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    
    if (self.dataList[indexPath.section][indexPath.row][@"controller"]){
        UIViewController *vc = [[self.dataList[indexPath.section][indexPath.row][@"controller"] alloc] init];
        
        vc.title = self.dataList[indexPath.section][indexPath.row][@"title"];
        
//        vc.view.backgroundColor = XLColor(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255));
        
        
        [self.navigationController pushViewController:vc animated:YES];
        self.navigationController.navigationBar.hidden = NO;
    }else{
        
//        
//        [tableView deselectRowAtIndexPath:indexPath animated:YES];
//        
//        [[SDImageCache sharedImageCache] clearDisk];
//        [[SDWebImageManager sharedManager] cancelAll];
//        
//        cell.detailTextLabel.text = [self caCheSize];
    }
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return !section ? 1 : CGFLOAT_MIN;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    if (self.isShowWave) {
        [self.waveView starWave];
    }
}

- (void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    
    if (fabs(offsetY) > 20) {
        self.showWave = YES;
    }
    else {
        self.showWave = NO;
    }
    
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    [self.waveView stopWave];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat offsetY = scrollView.contentOffset.y;
    
    if (offsetY < 0) {
        
        self.waveView.frame = CGRectMake(offsetY/2, offsetY, MTScreenW - offsetY, 270 - offsetY);  // 修改头部的frame值就行了
    }
    
}
@end
